package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.entity.Message;
import cn.yj.cgs.dba.mapper.MessageMapper;
import cn.yj.commons.utils.MapUtils;
import cn.yj.core.business.service.iservice.IMessageService;
import cn.yj.params.check.annotation.Require;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MessageService extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Override
    public boolean read(@Require String userCode, @Require List<Integer> messageIds) {
        messageIds.forEach(messageId -> {
            if (!baseMapper.isRead(userCode, messageId)) {
                baseMapper.toRead(userCode, messageId);
            }
        });
        return true;
    }

    @Override
    public long countNoRead(String userCode) {
        return baseMapper.noReadCount(userCode);
    }

    @Override
    public void notice(Message message) {
        try {
            baseMapper.insert(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Page<Message> pageList(@Require Map<String, Object> params, Page<Message> page) {
        baseMapper.pageList(params, page);
        List<Message> rows = page.getRows();
        rows.forEach(message -> message.setRead(baseMapper.isRead(MapUtils.getStringVal(params, "userCode"),
                message.getId())));
        return page;
    }


}
